package hauwei.exam416;

import java.util.*;

public class MaxIP {
    static class IPRange {
        String start;
        String end;
        int []startPoints;
        int []endPoints;
        int range;

        public IPRange(String start, String end) {
            this.start = start;
            this.end = end;
            setPoints();
        }

        public void setPoints(){
            String[] split1 = start.split("\\.");
            String[] split2 = end.split("\\.");

            startPoints = new int[4];
            endPoints = new int[4];
            for(int i=0;i<4;i++){
                startPoints[i] = Integer.parseInt(split1[i]);
                endPoints[i] = Integer.parseInt(split2[i]);
            }
            setRange();
        }

        public void setRange(){
            range = 0;
            for (int i=0;i<4;i++){
                range += (endPoints[i]-startPoints[i])*256;
            }
        }

    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        TreeSet<IPRange> ips =new TreeSet<>((a,b)->{
            for (int i=0;i<4;i++){
                if (a.endPoints[i]>b.endPoints[i]){
                    return 1;
                }
                if(a.endPoints[i]<b.endPoints[i]){
                    return -1;
                }
            }
            return 0;
        });
        scanner.nextLine();
        for (int i = 0; i < n; i++){
            String line = scanner.nextLine();
            String[] split = line.split(" ");
            ips.add(new IPRange(split[0],split[1]));
        }
        scanner.close();
        ips.forEach(ip->{
            System.out.println(ip.start+" "+ip.end);
        });
        List<IPRange>reIps=new ArrayList<>();

        ips.forEach(ip->{
            if(reIps.isEmpty()){
                reIps.add(ip);
            }else{
                IPRange lastIp=reIps.get(reIps.size()-1);
                int[]lastEndPoints = lastIp.endPoints;
                int[]nowStartPoints = ip.startPoints;
                if(isBigger(lastEndPoints,nowStartPoints)){
                    reIps.add(ip);
                }
            }
        });
        reIps.forEach(ip->{
            System.out.println(ip.start+" "+ip.end);
        });
    }
    public static boolean isBigger(int []a,int[]b){
        for (int i=0;i<4;i++){
            if(a[i]>b[i]){
                return true;
            }
            if(a[i]<b[i]){
                return false;
            }
        }
        return false;
    }
}
